using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model.FacManage.Dto;
using ZR.Model.FacManage;
using ZR.Repository;
using ZR.Service.FacManage.IFacManageService;
using Infrastructure;

namespace ZR.Service.FacManage
{
    /// <summary>
    /// 库区信息Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IFacWhseService), ServiceLifetime = LifeTime.Transient)]
    public class FacWhseService : BaseService<FacWhse>, IFacWhseService
    {
        /// <summary>
        /// 查询库区信息列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<FacWhseDto> GetPageList(FacWhseQueryDto parm)
        {
            var predicate = QueryExp(parm);
            var response = new PagedInfo<FacWhseDto>();
            var total = 0;
            response.PageSize = parm.PageSize;
            response.PageIndex = parm.PageNum;
            response.Result = Queryable().Where(predicate.ToExpression())
                .LeftJoin<FacFactory>((wa, tory) => wa.FactoryId == tory.FactoryId)
                .OrderByPropertyNameIF(parm.Sort.IsNotEmpty(), parm.Sort, parm.SortType.Contains("desc") ? OrderByType.Desc : OrderByType.Asc)
                .Select((wa, tory) => new FacWhseDto
                {
                    WhseId = wa.WhseId,
                    WhseCode = wa.WhseCode,
                    WhseName = wa.WhseName,
                    Enabled = wa.Enabled,
                    Remark = wa.Remark,
                    CreateBy = wa.CreateBy,
                    CreateTime = wa.CreateTime,
                    UpdateBy = wa.UpdateBy,
                    UpdateTime = wa.UpdateTime,
                    FactoryId = wa.FactoryId,
                    FactoryInfo = tory.FactoryCode + "  " + tory.FactoryName,
                }).ToPageList(parm.PageNum, parm.PageSize, ref total);
            response.TotalNum = total;
            return response;
        }

        /// <summary>
        /// 获取库区树信息
        /// </summary>
        /// <param name="disabledFactory">禁用工厂选择</param>
        /// <returns></returns>
        public List<TreeSelectInfo> GetTreeList(bool disabledFactory)
        {
            List<TreeSelectInfo> trees = new List<TreeSelectInfo>();
            //获取工厂信息
            List<FacFactory> factoryList = this.Context.Queryable<FacFactory>().ToList();
            //获取库区信息
            List<FacWhse> whseList = this.Context.Queryable<FacWhse>().ToList();
            foreach (FacFactory fac in factoryList)
            {
                TreeSelectInfo tree = new TreeSelectInfo();
                tree.value = fac.FactoryId;
                tree.label = fac.FactoryCode + " " + fac.FactoryName;
                tree.disabled = disabledFactory;
                foreach (FacWhse wa in whseList.Where(it => it.FactoryId == fac.FactoryId).ToList())
                {
                    tree.children.Add(new TreeSelectInfo()
                    {
                        value = wa.WhseId,
                        label = wa.WhseCode + " " + wa.WhseName
                    });
                }
                trees.Add(tree);
            }
            return trees;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="WhseId"></param>
        /// <returns></returns>
        public FacWhse GetInfo(string WhseId)
        {
            var response = Queryable()
                .Where(x => x.WhseId == WhseId)
                .First();

            return response;
        }

        /// <summary>
        /// 添加库区信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public FacWhse AddFacWhse(FacWhse model)
        {
            if (this.Any(it => it.WhseCode == model.WhseCode))
            {
                throw new CustomException($"系统中已包含{model.WhseCode}编码信息");
            }
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改库区信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateFacWhse(FacWhse model)
        {
            if (this.Any(it => it.WhseCode == model.WhseCode && it.WhseId != model.WhseId))
            {
                throw new CustomException($"系统中已包含{model.WhseCode}编码信息");
            }
            return Update(model, true);
        }

        /// <summary>
        /// 删除库区信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteFacWhse(string[] ids)
        {
            if (this.Context.Queryable<FacLoc>().Any(it => ids.Contains(it.WhseId)))
            {
                throw new CustomException($"系统中已经存在此库区的货位信息，不能删除");
            }
            return Delete(ids);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<FacWhse> QueryExp(FacWhseQueryDto parm)
        {
            var predicate = Expressionable.Create<FacWhse>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.WhseCode), it => it.WhseCode == parm.WhseCode);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.WhseName), it => it.WhseName.Contains(parm.WhseName));
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.FactoryId), it => it.FactoryId == parm.FactoryId);
            predicate = predicate.AndIF(parm.Enabled != null, it => it.Enabled == parm.Enabled);

            return predicate;
        }
    }
}